package com.bibrApp.difu.actvity.security_strategy_mvp;

import android.content.Context;
import android.text.TextUtils;
import android.widget.TextView;

import com.bibrApp.difu.Response.LoginResponseBody;
import com.bibrApp.difu.api.RetrofitNet;
import com.bibrApp.difu.base.BaseObserver;
import com.bibrApp.difu.base.BasePresenter;
import com.bibrApp.difu.utils.EncryptUtils;
import com.bibrApp.difu.utils.ToastUtils;
import com.google.gson.Gson;

import java.util.HashMap;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class SecurityPresenter extends BasePresenter<ISecurityView> {
    public SecurityPresenter(Context context, ISecurityView iView) {
        super(context, iView);
    }

    /**
     * @param passwordStatus
     * @param passwordStrategy passwordStatus	String	否	交易密码校验级别（默认1：每日首次交易需要密码，0：每次交易都要密码）
     *                         passwordStrategy	String	否	密码安全策略—修改密码后24小时禁止提币（默认1：开启，0：关闭）
     */
    public void setSecurityStrategy(String passwordStatus, String passwordStrategy) {
        HashMap<String, Object> map = new HashMap<>();
        if(!TextUtils.isEmpty(passwordStatus)){
            map.put("passwordStatus", passwordStatus);
        }
        if(!TextUtils.isEmpty(passwordStrategy)){
            map.put("passwordStrategy", passwordStrategy);
        }

        RetrofitNet.getInstance()
                .getApi()
                .updateStrategy(EncryptUtils.encryptNewType(map))
                .compose(getActivityLifecycleProvider().bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new BaseObserver<Object>(context) {

                    @Override
                    protected void onSuccess(Object value) {
                        iView.success("设置成功");
                    }

                    @Override
                    protected void onFialure(int code, String msg) {
                        super.onFialure(code, msg);
                        iView.error(msg);
                    }
                });
    }

    public void getUserInfo(String userId) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        RetrofitNet.getInstance().getApi()
                .getAccountInfo(RequestBody.create(MediaType.parse("application/json"), new Gson().toJson(map)))
                .compose(getActivityLifecycleProvider().bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new BaseObserver<Object>(context) {
                    @Override
                    protected void onSuccess(Object value) {
                        String jsonStr = EncryptUtils.objectToJson(value);
                        LoginResponseBody.CenterUserMain loginRes = new Gson().fromJson(jsonStr, LoginResponseBody.CenterUserMain.class);
                        iView.successOfGetUserInfo(loginRes);
                    }

                    @Override
                    protected void onFialure(int code, String msg) {
                        super.onFialure(code, msg);
                    }
                });

    }
}
